package Models;

import java.util.LinkedList;
import java.util.List;
import Export.*;
import Interfaces.*;

public class User implements IDBItem {

	boolean isNew, isModified, isAdmin;
	
	int idUser;
	String login, password, email;
	List<Aplikace> childs;
	
	static String deleteString = "DELETE FROM" + CodeStorageContext.UserTable + " OUTPUT DELETED.IDUser WHERE IDUser=";
	public static final String selectUser = "SELECT [IDUser], [Login], [Password], [Email], [IsAdmin] FROM "+ CodeStorageContext.UserTable +" WHERE ";
	public static final String selectUserList = "SELECT [IDUser], [Login], [Password], [Email], [IsAdmin] FROM "+ CodeStorageContext.UserTable;

	public User(int idUser, String email, String login, String password, boolean isAdmin){
		this(idUser, email, login, password, isAdmin, null);
	}
	
	public User(int idUser, String email, String login, String password, boolean isAdmin, List<Aplikace> childs){
		isModified = false;	
		isNew = idUser < 0 ? true : false;
		this.idUser = idUser;
		this.login = login;
		this.password = password;
		this.email = email;
		this.isAdmin = isAdmin;
		this.childs = childs;
	}

	public User(String email, String login, String password, boolean isAdmin){
		this(-1, email, login, password, isAdmin, null);
	}

	public boolean isModified() {
		return isModified;
	}

	public boolean isNew() {
		return isNew;
	}

	public int  getMasterObjectID(){
		return -1;
	}	
	
	public IDBItem getMasterObject() {
		return null;
	}
	
	public String GetEmail(){
		return email;
	}
	

	public void SetEmail(String email){
		this.email = email;
	}
	
	public void setMasterObject(IDBItem masterObject){}

	public List<IDBItem> getChilds() {
		if(childs != null)
		{
			List<IDBItem> output = new LinkedList<IDBItem>();
			for(Aplikace item : childs)
				output.add((IDBItem)item);
			return output;
		}
		return null;
	}
	
	public void setChilds(List<IDBItem> childs){
		if(childs != null && !childs.isEmpty()){
			this.childs = new LinkedList<Aplikace>();
			for(IDBItem app : childs)
				this.childs.add((Aplikace) app);
		}
	}
	
	public String GetLogin(){
		return login;
	}
	
	public void SetLogin(String value){
		if(value.compareTo(login) != 0){
			isModified = true;
			login = value;
		}
	}
	
	public boolean GetIsAdmin(){
		return isAdmin;
	}
	
	public void SetIsAdmin(boolean value){
		if(isAdmin == value){
			isModified = true;
			isAdmin = value;
		}
	}
	
	public String GetPassword(){
		return password;
	}
	
	public void SetPassword(String value){
		if(value.compareTo(password) != 0){
			isModified = true;
			password = value;
		}
	}
	
	public String saveParams() {
		StringBuilder sb = new StringBuilder();
		if(isNew){
			sb.append("INSERT INTO ");
			sb.append(CodeStorageContext.UserTable);
			sb.append(" ([Login], [Password], [Email], [IsAdmin])");
			sb.append(" OUTPUT INSERTED.IDUser");
			sb.append(" VALUES (");
			sb.append("'" + login +"', ");
			sb.append("'" + password +"', ");
			sb.append("'" + email +"', ");
			sb.append(isAdmin ? "1" : "0");
			sb.append(")");
			return sb.toString();
		}
		else{
			if(isModified){
				sb.append("UPDATE ");
				sb.append(CodeStorageContext.UserTable);
				sb.append(" SET");
				sb.append(" [Login]=");
				sb.append("'" + login + "', ");
				sb.append(" [Password]=");
				sb.append("'" + password + "', ");
				sb.append(" [Email]=");
				sb.append("'" + email + "', ");
				sb.append(" [IsAdmin]=");
				sb.append(isAdmin ? "1" : "0");
				sb.append(" OUTPUT INSERTED.IDUser");
				sb.append(" WHERE [IDUser]=");
				sb.append(idUser);
				return sb.toString();
			}
		}
		return null;
			
	}

	public String deleteParams() {
		
		if(!isNew)
			return deleteString + idUser;
		
		return null;
	}

	public int getID() {
		return idUser;
	}

	public void setID(int id) {
		if(id > 0){
			isNew = false;
			isModified = false;
			idUser = id;
		}
			
	}
	

}
